﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using CRM.Utility.Logging;

namespace CRM.Api
{
    /// <summary>
    /// 
    /// </summary>
    public class WebApiApplication : System.Web.HttpApplication
    {
        /// <summary>
        /// 应用程序配置
        /// </summary>
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
           
        }

        /// <summary>
        /// 跨域
        /// </summary>
        protected void Application_BeginRequest()
        {            
            //是否是简单请求或同域名请求
            if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.Headers.Add("Access-Control-Allow-Headers", "*");
                HttpContext.Current.Response.Headers.Add("Access-Control-Allow-Methods", "*");
                //响应头添加Access-Control-Allow-Origin *是全部允许，如果允许某个域名，则直接写域名 
                HttpContext.Current.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                //HttpContext.Current.Response.Headers.Add("Access-Control-Max-Age", "-1");
                Response.End();
            }
        }
    }
}
